<template>
  <div class="container">
  	<div class="handle-box">
    	<div style="width: 80%">
		    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
		      <el-form-item label="名称" prop="name">
		        <el-input
		          v-model="queryParams.name"
		          placeholder="请输入名称"
		          clearable
		          size="small"
		          @keyup.enter.native="handleQuery"
		        />
		      </el-form-item>
		      <el-form-item label="性别" prop="sex">
		        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          		  <el-option key="" value="">请选择</el-option>
		          <el-option
		            v-for="dict in sexOptions"
		            :key="dict.code"
		            :label="dict.value"
		            :value="dict.code"
		          />
		        </el-select>
		      </el-form-item>
		      <el-form-item label="文本" prop="text">
		        <el-input
		          v-model="queryParams.text"
		          placeholder="请输入文本"
		          clearable
		          size="small"
		          @keyup.enter.native="handleQuery"
		        />
		      </el-form-item>
		      <el-form-item label="时间" prop="date">
		        <el-date-picker clearable size="small" style="width: 200px"
		          v-model="queryParams.date"
		          type="date"
		          value-format="yyyy-MM-dd"
		          placeholder="选择时间">
		        </el-date-picker>
		      </el-form-item>
		      <el-form-item>
		        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
		      </el-form-item>
		    </el-form>
	    </div>
	    <div>
		    <el-row :gutter="10" class="mb8 el-form-item">
		      <el-col :span="1.5">
		        <el-button
		          type="primary"
		          icon="el-icon-plus"
		          size="mini"
		          @click="handleAdd"
		        >新增</el-button>
		      </el-col>
		      <el-col :span="1.5">
		        <el-button
		          type="success"
		          icon="el-icon-edit"
		          size="mini"
		          :disabled="single"
		          @click="handleUpdate"
		        >修改</el-button>
		      </el-col>
		      <el-col :span="1.5">
		        <el-button
		          type="danger"
		          icon="el-icon-delete"
		          size="mini"
		          :disabled="multiple"
		          @click="handleDelete"
		        >删除</el-button>
		      </el-col>
		    </el-row>
		</div>
	</div>

    <el-table :data="testList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center"  :show-overflow-tooltip="true" prop="id" />
      <el-table-column  :show-overflow-tooltip="true" label="名称" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />
      <el-table-column  :show-overflow-tooltip="true" label="文本" align="center" prop="text" />
      <el-table-column label="时间" align="center"  :show-overflow-tooltip="true" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ $tool.parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination">
        <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page="queryParams.pageIndex"
                :page-size="queryParams.pageSize"
                :total="pageTotal"
                @current-change="handlePageChange"
        ></el-pagination>
    </div>

    <!-- 添加或修改测试对话框 -->
    <el-dialog :title="title"  :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
          	<el-option key="" value="">请选择</el-option>
            <el-option
              v-for="dict in sexOptions"
              :key="dict.code"
              :label="dict.value"
:value="parseInt(dict.code)"
/>
          </el-select>
        </el-form-item>
        <el-form-item label="文本" prop="text">
          <el-input v-model="form.text" placeholder="请输入文本" />
        </el-form-item>
        <el-form-item label="时间" prop="date">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.date"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import test from "@/api/test";

export default {
  name: "Test",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 测试表格数据
      testList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 性别字典
      sexOptions: [],
      pageTotal:0,
      // 分页
      page:{
        current:0,
        size:10,
        total: 0,
      },
      // 查询参数
      queryParams: {
        name: null,
        sex: null,
        text: null,
        date: null,
 		pageIndex: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],

        sex: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],

      }
    };
  },
  created() {
    this.getList();
    vue.$dictionary.getDictionarys("SEX").then(response => {
      this.sexOptions = response.data;
    });
  },
  methods: {
    /** 分页导航 */
    handlePageChange(val) {
        this.$set(this.queryParams, 'pageIndex', val);
        this.getList();
    },
    /** 查询测试列表 */
    getList() {
      this.loading = true;
      test.pageTest(this.queryParams).then(response => {
          let res = response.data
        this.testList = res.records;
        this.pageTotal = res.total;
        this.loading = false;
      });
    },
    // 性别字典翻译
    sexFormat(row, column) {
      return vue.$tool.selectDictLabel(this.sexOptions, row.sex);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sex: null,
        text: null,
        date: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
       vue.$tool.resetForm(this.form);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.page.current = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加测试";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      test.getTest(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改测试";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null && this.form.id != "") {
              test.updateTest(this.form).then(response => {
                  if(response.code==200) {
                      this.$message.success("修改成功");
                      this.open = false;
                      this.getList();
                  }else {
                      this.msgError(response.message);
                  }
            });
          } else {
              test.addTest(this.form).then(response => {
                  if(response.code==200) {
                      this.$message.success("新增成功");
                      this.open = false;
                      this.getList();
                  }else {
                      this.msgError(response.message);
                  }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return test.delTest(ids);
        }).then((response) => {
          if(response.code==200) {
              this.getList();
              this.msgSuccess("删除成功");
          }else {
              this.msgError(response.message);
          }
        })
    },
  }
};
</script>

<style scoped>
    .handle-box {
        display: flex;
        display: -webkit-flex;
        justify-content:space-between;
        -webkit-justify-content:space-between;
        align-items: center;
        -webkit-align-items: center;
    }


</style>
